package com.example.login;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    EditText user;
    EditText password;
    Button register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.layout);
        user = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.password);
        Log.e( "TAG", "onCreate: ");
    }
    void jump(){
        Intent intent = new Intent(MainActivity.this, MainActivity2.class);
        startActivity(intent);
    }
    public void login(View view) {
        FileInputStream fis = null;
        String userid = user.getText().toString().trim();
        String pswd = password.getText().toString().trim();
        String[] info;
        try {
            fis = openFileInput("info.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            String s = new String(buffer);
            info = s.split(":");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if(userid.equals(info[0]) && pswd.equals(info[1])){
            jump();
            Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(MainActivity.this, "账号密码错误，请重新输入", Toast.LENGTH_SHORT).show();
        }

    }

    public void register(View view){
        Log.e("TAG", "register: ");
        String userid = user.getText().toString().trim();
        String pswd = password.getText().toString().trim();
        if (userid.isEmpty() || pswd.isEmpty()) {
            Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return; // 退出方法
        }
        String info = userid + ":" + pswd;
        Log.e("TAG", "register: " + info );
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("info.txt", MODE_PRIVATE);
            fos.write(info.getBytes());
            Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos != null)
                {
                    fos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        user.setText("");
        password.setText("");
    }
    public void cancel(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("退出");
        builder.setMessage("是否要退出程序");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.create().show();
    }

}